home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / Emacs / file.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  13KB  |  362 lines

  1. /* file.c */
  2.  
  3. /*
  4.  * The routines in this file
  5.  * handle the reading and writing of
  6.  * disk files. All of details about the
  7.  * reading and writing of the disk are
  8.  * in "fileio.c".
  9.  */
  10. #include        <stdio.h>
  11. #include        "ed.h"
  12.  
  13. /*
  14.  * Read a file into the current
  15.  * buffer. This is really easy; all you do it
  16.  * find the name of the file, and call the standard
  17.  * "read a file into the current buffer" code.
  18.  * Bound to "C-X C-R".
  19.  */
  20. fileread(f, n)
  21. {
  22.         register int    s;
  23.         char            fname[NFILEN];
  24.  
  25.         if ((s=mlreply("Read file: ", fname, NFILEN)) != TRUE)
  26.                 return (s);
  27.         return (readin(fname));
  28. }
  29.  
  30. /*
  31.  * Select a file for editing.
  32.  * Look around to see if you can find the
  33.  * fine in another buffer; if you can find it
  34.  * just switch to the buffer. If you cannot find
  35.  * the file, create a new buffer, read in the
  36.  * text, and switch to the new buffer.
  37.  * Bound to C-X C-V.
  38.  */
  39. filevisit(f, n)
  40. {
  41.         register BUFFER *bp;
  42.         register WINDOW *wp;
  43.         register LINE   *lp;
  44.         register int    i;
  45.         register int    s;
  46.         char            bname[NBUFN];
  47.         char            fname[NFILEN];
  48.  
  49.         if ((s=mlreply("Visit file: ", fname, NFILEN)) != TRUE)
  50.                 return (s);
  51.         for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  52.                 if ((bp->b_flag&BFTEMP)==0 && strcmp(bp->b_fname, fname)==0) {
  53.                         if (--curbp->b_nwnd == 0) {
  54.                                 curbp->b_dotp  = curwp->w_dotp;
  55.                                 curbp->b_doto  = curwp->w_doto;
  56.                                 curbp->b_markp = curwp->w_markp;
  57.                                 curbp->b_marko = curwp->w_marko;
  58.                         }
  59.                         curbp = bp;
  60.                         curwp->w_bufp  = bp;
  61.                         if (bp->b_nwnd++ == 0) {
  62.                                 curwp->w_dotp  = bp->b_dotp;
  63.                                 curwp->w_doto  = bp->b_doto;
  64.                                 curwp->w_markp = bp->b_markp;
  65.                                 curwp->w_marko = bp->b_marko;
  66.                         } else {
  67.                                 wp = wheadp;
  68.                                 while (wp != NULL) {
  69.                                         if (wp!=curwp && wp->w_bufp==bp) {
  70.                                                 curwp->w_dotp  = wp->w_dotp;
  71.                                                 curwp->w_doto  = wp->w_doto;
  72.                                                 curwp->w_markp = wp->w_markp;
  73.                                                 curwp->w_marko = wp->w_marko;
  74.                                                 break;
  75.                                         }
  76.                                         wp = wp->w_wndp;
  77.                                 }
  78.                         }
  79.                         lp = curwp->w_dotp;
  80.                         i = curwp->w_ntrows/2;
  81.                         while (i-- && lback(lp)!=curbp->b_linep)
  82.                                 lp = lback(lp);
  83.                         curwp->w_linep = lp;
  84.                         curwp->w_flag |= WFMODE|WFHARD;
  85.                         mlwrite("[Old buffer]");
  86.                         return (TRUE);
  87.                 }
  88.         }
  89.         makename(bname, fname);                 /* New buffer name.     */
  90.         while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  91.                 s = mlreply("Buffer name: ", bname, NBUFN);
  92.                 if (s == ABORT)                 /* ^G to just quit      */
  93.                         return (s);
  94.                 if (s == FALSE) {               /* CR to clobber it     */
  95.                         makename(bname, fname);
  96.                         break;
  97.                 }
  98.         }
  99.         if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  100.                 mlwrite("Cannot create buffer");
  101.                 return (FALSE);
  102.         }
  103.         if (--curbp->b_nwnd == 0) {             /* Undisplay.           */
  104.                 curbp->b_dotp = curwp->w_dotp;
  105.                 curbp->b_doto = curwp->w_doto;
  106.                 curbp->b_markp = curwp->w_markp;
  107.                 curbp->b_marko = curwp->w_marko;
  108.         }
  109.         curbp = bp;                             /* Switch to it.        */
  110.         curwp->w_bufp = bp;
  111.         curbp->b_nwnd++;
  112.         return (readin(fname));                 /* Read it in.          */
  113. }
  114.  
  115. /*
  116.  * Read file "fname" into the current
  117.  * buffer, blowing away any text found there. Called
  118.  * by both the read and visit commands. Return the final
  119.  * status of the read. Also called by the mainline,
  120.  * to read in a file specified on the command line as
  121.  * an argument.
  122.  */
  123. readin(fname)
  124. char    fname[];
  125. {
  126.         register LINE   *lp1;
  127.         register LINE   *lp2;
  128.         register int    i;
  129.         register WINDOW *wp;
  130.         register BUFFER *bp;
  131.         register int    s;
  132.         register int    nbytes;
  133.         register int    nline;
  134.         char            line[NLINE];
  135.  
  136.         bp = curbp;                             /* Cheap.               */
  137.         if ((s=bclear(bp)) != TRUE)             /* Might be old.        */
  138.                 return (s);
  139.         bp->b_flag &= ~(BFTEMP|BFCHG);
  140.         strcpy(bp->b_fname, fname);
  141.         if ((s=ffropen(fname)) == FIOERR)       /* Hard file open.      */
  142.                 goto out;
  143.         if (s == FIOFNF) {                      /* File not found.      */
  144.                 mlwrite("[New file]");
  145.                 goto out;
  146.         }
  147.         mlwrite("[Reading file]");
  148.         nline = 0;
  149.         while ((s=ffgetline(line, NLINE)) == FIOSUC) {
  150.                 nbytes = strlen(line);
  151.                 if ((lp1=lalloc(nbytes)) == NULL) {
  152.                         s = FIOERR;             /* Keep message on the  */
  153.                         break;                  /* display.             */
  154.                 }
  155.                 lp2 = lback(curbp->b_linep);
  156.                 lp2->l_fp = lp1;
  157.                 lp1->l_fp = curbp->b_linep;
  158.                 lp1->l_bp = lp2;
  159.                 curbp->b_linep->l_bp = lp1;
  160.                 for (i=0; i<nbytes; ++i)
  161.                         lputc(lp1, i, line[i]);
  162.                 ++nline;
  163.         }
  164.         ffclose();                              /* Ignore errors.       */
  165.         if (s == FIOEOF) {                      /* Don't zap message!   */
  166.                 if (nline == 1)
  167.                         mlwrite("[Read 1 line]");
  168.                 else
  169.                         mlwrite("[Read %d lines]", nline);
  170.         }
  171. out:
  172.         for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  173.                 if (wp->w_bufp == curbp) {
  174.                         wp->w_linep = lforw(curbp->b_linep);
  175.                         wp->w_dotp  = lforw(curbp->b_linep);
  176.                         wp->w_doto  = 0;
  177.                         wp->w_markp = NULL;
  178.                         wp->w_marko = 0;
  179.                         wp->w_flag |= WFMODE|WFHARD;
  180.                 }
  181.         }
  182.         if (s == FIOERR)                        /* False if error.      */
  183.                 return (FALSE);
  184.         return (TRUE);
  185. }
  186.  
  187. /*
  188.  * Take a file name, and from it
  189.  * fabricate a buffer name. This routine knows
  190.  * about the syntax of file names on the target system.
  191.  * I suppose that this information could be put in
  192.  * a better place than a line of code.
  193.  */
  194. makename(bname, fname)
  195. char    bname[];
  196. char    fname[];
  197. {
  198.         register char   *cp1;
  199.         register char   *cp2;
  200.  
  201.         cp1 = &fname[0];
  202.         while (*cp1 != 0)
  203.                 ++cp1;
  204.  
  205. #if     AMIGA
  206.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  207.                 --cp1;
  208. #endif
  209. #if     VMS
  210.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  211.                 --cp1;
  212. #endif
  213. #if     CPM
  214.         while (cp1!=&fname[0] && cp1[-1]!=':')
  215.                 --cp1;
  216. #endif
  217. #if     MSDOS
  218.         while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  219.                 --cp1;
  220. #endif
  221. #if     V7
  222.         while (cp1!=&fname[0] && cp1[-1]!='/')
  223.                 --cp1;
  224. #endif
  225.         cp2 = &bname[0];
  226.         while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  227.                 *cp2++ = *cp1++;
  228.         *cp2 = 0;
  229. }
  230.  
  231. /*
  232.  * Ask for a file name, and write the
  233.  * contents of the current buffer to that file.
  234.  * Update the remembered file name and clear the
  235.  * buffer changed flag. This handling of file names
  236.  * is different from the earlier versions, and
  237.  * is more compatable with Gosling EMACS than
  238.  * with ITS EMACS. Bound to "C-X C-W".
  239.  */
  240. filewrite(f, n)
  241. {
  242.         register WINDOW *wp;
  243.         register int    s;
  244.         char            fname[NFILEN];
  245.  
  246.         if ((s=mlreply("Write file: ", fname, NFILEN)) != TRUE)
  247.                 return (s);
  248.         if ((s=writeout(fname)) == TRUE) {
  249.                 strcpy(curbp->b_fname, fname);
  250.                 curbp->b_flag &= ~BFCHG;
  251.                 wp = wheadp;                    /* Update mode lines.   */
  252.                 while (wp != NULL) {
  253.                         if (wp->w_bufp == curbp)
  254.                                 wp->w_flag |= WFMODE;
  255.                         wp = wp->w_wndp;
  256.                 }
  257.         }
  258.         return (s);
  259. }
  260.  
  261. /*
  262.  * Save the contents of the current
  263.  * buffer in its associatd file. No nothing
  264.  * if nothing has changed (this may be a bug, not a
  265.  * feature). Error if there is no remembered file
  266.  * name for the buffer. Bound to "C-X C-S". May
  267.  * get called by "C-Z".
  268.  */
  269. filesave(f, n)
  270. {
  271.         register WINDOW *wp;
  272.         register int    s;
  273.  
  274.         if ((curbp->b_flag&BFCHG) == 0)         /* Return, no changes.  */
  275.                 return (TRUE);
  276.         if (curbp->b_fname[0] == 0) {           /* Must have a name.    */
  277.                 mlwrite("No file name");
  278.                 return (FALSE);
  279.         }
  280.         if ((s=writeout(curbp->b_fname)) == TRUE) {
  281.                 curbp->b_flag &= ~BFCHG;
  282.                 wp = wheadp;                    /* Update mode lines.   */
  283.                 while (wp != NULL) {
  284.                         if (wp->w_bufp == curbp)
  285.                                 wp->w_flag |= WFMODE;
  286.                         wp = wp->w_wndp;
  287.                 }
  288.         }
  289.         return (s);
  290. }
  291.  
  292. /*
  293.  * This function performs the details of file
  294.  * writing. Uses the file management routines in the
  295.  * "fileio.c" package. The number of lines written is
  296.  * displayed. Sadly, it looks inside a LINE; provide
  297.  * a macro for this. Most of the grief is error
  298.  * checking of some sort.
  299.  */
  300. writeout(fn)
  301. char    *fn;
  302. {
  303.         register int    s;
  304.         register LINE   *lp;
  305.         register int    nline;
  306.  
  307.         if ((s=ffwopen(fn)) != FIOSUC)          /* Open writes message. */
  308.                 return (FALSE);
  309.         lp = lforw(curbp->b_linep);             /* First line.          */
  310.         nline = 0;                              /* Number of lines.     */
  311.         while (lp != curbp->b_linep) {
  312.                 if ((s=ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  313.                         break;
  314.                 ++nline;
  315.                 lp = lforw(lp);
  316.         }
  317.         if (s == FIOSUC) {                      /* No write error.      */
  318.                 s = ffclose();
  319.                 if (s == FIOSUC) {              /* No close error.      */
  320.                         if (nline == 1)
  321.                                 mlwrite("[Wrote 1 line]");
  322.                         else
  323.                                 mlwrite("[Wrote %d lines]", nline);
  324.                 }
  325.         } else                                  /* Ignore close error   */
  326.                 ffclose();                      /* if a write error.    */
  327.         if (s != FIOSUC)                        /* Some sort of error.  */
  328.                 return (FALSE);
  329.         return (TRUE);
  330. }
  331.  
  332. /*
  333.  * The command allows the user
  334.  * to modify the file name associated with
  335.  * the current buffer. It is like the "f" command
  336.  * in UNIX "ed". The operation is simple; just zap
  337.  * the name in the BUFFER structure, and mark the windows
  338.  * as needing an update. You can type a blank line at the
  339.  * prompt if you wish.
  340.  */
  341. filename(f, n)
  342. {
  343.         register WINDOW *wp;
  344.         register int    s;
  345.         char            fname[NFILEN];
  346.  
  347.         if ((s=mlreply("Name: ", fname, NFILEN)) == ABORT)
  348.                 return (s);
  349.         if (s == FALSE)
  350.                 strcpy(curbp->b_fname, "");
  351.         else
  352.                 strcpy(curbp->b_fname, fname);
  353.         wp = wheadp;                            /* Update mode lines.   */
  354.         while (wp != NULL) {
  355.                 if (wp->w_bufp == curbp)
  356.                         wp->w_flag |= WFMODE;
  357.                 wp = wp->w_wndp;
  358.         }
  359.         return (TRUE);
  360. }
  361.  
  362.